package com.eleven.trades.controller;


import com.alibaba.fastjson.JSONObject;
import com.eleven.trades.error.BusinessException;
import com.eleven.trades.result.JsonResult;
import com.eleven.trades.result.JsonResultFactory;
import com.eleven.trades.result.ResultCode;
import com.eleven.trades.server.AccountServer;
import com.eleven.trades.server.LoginAndExitServer;
import com.eleven.trades.serverimpl.UserServerImpl;
import com.eleven.trades.vo.AccountBalance;
import com.eleven.trades.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServerImpl userServer;
    @Autowired
    private LoginAndExitServer loginAndExitServer;

    @Autowired
    AccountServer accountServer;

    @Autowired
    private HttpServletRequest httpServletRequest;
    @PostMapping("/register")
    public JsonResult register(@RequestBody UserVo userVo) throws BusinessException {

        userServer.register(userVo);
        return JsonResultFactory.getInstance().buildSuccessResult();
    }
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserVo userVo) throws BusinessException {

        //用户登录服务，用来检验用户登录是否合法
//        User user =loginAndExitServer.login(userVo);
        int user = loginAndExitServer.login(userVo);
        //将登录凭证加入到用户登录成功的session内,后续如果要检验用户受否已登录，可以通过，取得is_login是否为true
       this.httpServletRequest.getSession().setAttribute("IS_LOGIN",true);
       this.httpServletRequest.getSession().setAttribute("LOGIN)USER",user);
        switch (user){
            case -1:
                return JsonResultFactory.getInstance().buildFailResult(ResultCode.USER_PHONE_NOT_EXIST);
            case 0:
                return JsonResultFactory.getInstance().buildFailResult(ResultCode.USER_CREDENTIALS_ERROR);
            default:
                return JsonResultFactory.getInstance().buildSuccessResult(user);
        }
    }

    @GetMapping("/getInfo")
    public JsonResult getInfo(@RequestParam("id") Integer id){
        return JsonResultFactory.getInstance().buildSuccessResult(JSONObject.toJSONString(userServer.getUserInfo(id)));
    }

   @GetMapping("/")
    public void dd(@RequestBody UserVo userVo)
    {
        System.out.println("hellop"+userVo);
        return ;
    }

    @GetMapping("/getAccount")
    public JsonResult getAccount(@RequestParam("id") Integer id){
        try {
            List<AccountBalance> accountBalances = accountServer.selectAccountBalance(id);
            List<AccountBalance> res = new ArrayList<>();
            accountBalances.forEach( e-> res.add(new AccountBalance(e.getAccountId(), e.getAccountBalance())));
            String s = JSONObject.toJSONString(res);
            return JsonResultFactory.getInstance().buildSuccessResult(s);
        } catch (Exception e){
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.COMMON_FAIL);
        }
    }

}
